from django.shortcuts import render,redirect,HttpResponse
from django.views import View
from django.urls import reverse
from firstblood import models
# Create your views here.
class Home(View):
    def dispatch(self, request, *args, **kwargs):
        print('before')
        #调用父类的dispatch 方法
        result = super(Home,self).dispatch(request, *args, **kwargs)
        print('after')
        return result
    def get(self,request):
        print(request.method)
        return render(request, 'home.html')
    def post(self,request):
        print(request.method)
        return render(request,'home.html')

def login(request):
    error_message = ''
    if request.method == 'POST':
        user = request.POST.get('user',None)
        pwd = request.POST.get('pwd', None)
        models.Usergroup.objects.create(caption='DBA')
        obj = models.UserInfo.objects.filter(username=user,password=pwd)
        if obj:

            return redirect('/index/')
        else:
            error_message="密码错"
            return render(request,'login.html',{'error_message':error_message})
    if request.method == 'GET':
        return render(request, 'login.html')


def home(request):
    error_message = ''
    if request.method == 'POST':
        user = request.POST.get('user',None)
        email = request.POST.get('email', None)
        age = request.POST.get('age', None)
        users={'user':user,'email':email,'age':age}
        user_list.append(users)
        return render(request, 'home.html', {'user_list': user_list})
    if request.method == 'GET':
        return render(request, 'home.html',{'user_list':user_list})
user_dict = {
    '1':{'name':'cnsz01','ip':'10.0.0.1','address':'59066','mac':'95279623'},
    '2':{'name':'cnsz02','ip': '10.0.0.2', 'address':'59066','mac': '95279623'},
    '3':{'name':'cnsz03','ip':'10.0.0.3','address':'59066','mac':'95279623'},
}
def index(request):
    return render(request,'index.html')
def user(request):

    if request.method == "GET":
        user_list = models.UserInfo.objects.all()
        for i in user_list:
            print(i.user_group)
        return render(request,'user.html',{'user_list':user_list})
    elif request.method == "POST":
        u = request.POST.get('user')
        p = request.POST.get('password')
        models.UserInfo.objects.create(username=u,password=p)
        return redirect('/firstblood/user/')
def usergroup(request):
    return render(request,'usergroup.html')
def user_detail(request,user_id):

    user_list = models.UserInfo.objects.filter(id=user_id).first()
    return render(request,'user_detail.html',{'user_list':user_list})
def user_modify(request, user_id):
    if request.method == "GET":
        user_list = models.UserInfo.objects.filter(id=user_id).first()
        print(user_list.username)
        return render(request,'user_modify.html',{'user_list':user_list})

    elif request.method == "POST":
        print('卧槽')
        u = request.POST.get('user')
        p = request.POST.get('password')
        models.UserInfo.objects.filter(id=user_id).update(username=u,password=p)

        return redirect('/firstblood/user/')

def user_del(request, user_id):
    models.UserInfo.objects.filter(id=user_id).delete()
    return redirect('/firstblood/user/')
    #
# def detail(request):
#     nid = request.GET.get('nid')
#     detail_info = user_dict[nid]
#     return render(request,'detail.html',{'detail_info':detail_info})


def detail(request,*args,**kwargs):
    v = reverse('runs')
    print('reverse :',v)
    print(kwargs)
    print('进入')
    return HttpResponse(kwargs['nid'],kwargs['uid'])
    # detail_info = user_dict[nid]
    # print(detail_info)
    # return render(request,'detail.html',{'detail_info':detail_info})

# def put(request):
#     if request.method == 'POST':
#         obj = request.FILES.get('picture')
#         print(obj.name)
#         f = open(obj.name,mode='wb')
#         for i in obj.chunks():
#             f.write(i)
#         f.close()

def run(request,*args,**kwargs):
    v = reverse('runs',kwargs={"uid":1,"nid":2})  #使用指定参数形式
    # v = reverse('runs',args=(nid,)) #增加参数
    print('reverse :', v)
    print(request.path_info)
    return render(request,'run.html')


def orm(request):
    #数据库的增删改查
    #创建数据的两种方式
    #models.UserInfo.objects.create(username='root',password='123')
    # # obj = models.UserInfo(username='user1',password='ddd')
    # # obj.save()
    # return HttpResponse("ORM")

    #更新
    models.UserInfo.objects.filter(id=1).update(password='2')
    #查询

    result = models.UserInfo.objects.filter(username='alex')
    for row in result:
        print(row.password)

    #删除
    # models.UserInfo.objects.filter(username='alex').delete()
    # #models.UserInfo.objects.all().delete()
    return HttpResponse(result)


